from rocketmq import Producer


def main():
    # 创建生产者实例
    producer = Producer("your_producer_group")
    producer.set_name_server_address("10.10.1.96:9876")

    # 启动生产者
    producer.start()
    print("Producer started.")

    # 发送消息
    try:
        msg = producer.send_sync(
            topic="your_topic",
            body="Hello, RocketMQ!",
            key="your_message_key"  # 指定消息的 key
        )
        print(f"Sent message: {msg.id}, Key: {msg.key}")
    except Exception as e:
        print(f"Failed to send message: {e}")

    # 关闭生产者
    producer.shutdown()


if __name__ == "__main__":
    main()